package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.manage.cms.domain.HbJrcp;
import com.ruoyi.manage.cms.domain.vo.HbJrcpVo;
import com.ruoyi.manage.cms.service.IHbJrcpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.ObjUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 金融产品Controller
 *
 * @author wangtong
 * @date 2025-07-20
 */
@Api(tags="金融产品控制器")
@RestController
@RequestMapping("/manage/jrcp" )
public class HbJrcpController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IHbJrcpService hbJrcpService;

    /**
     * 查询金融产品列表
     */
    @ApiOperation(value = "查询金融产品",notes = "金融产品管理")
    @PreAuthorize("@ss.hasPermi('manage:jrcp:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(HbJrcpVo hbJrcp, QueryWrapper<HbJrcp> queryWrapper) {
        setQueryCondition(hbJrcp,queryWrapper);
        startPage();
        List<HbJrcp> list = hbJrcpService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(HbJrcpVo hbJrcp, QueryWrapper<HbJrcp> queryWrapper) {
        if(StrUtil.isNotBlank(hbJrcp.getInstitutionName())){
            queryWrapper.like(TABLE_PREFIX+"institution_name" ,hbJrcp.getInstitutionName());
        }
        if(StrUtil.isNotBlank(hbJrcp.getProductName())){
            queryWrapper.like(TABLE_PREFIX+"product_name" ,hbJrcp.getProductName());
        }
        if(StrUtil.isNotBlank(hbJrcp.getGuaranteeMode())){
            queryWrapper.eq(TABLE_PREFIX+"guarantee_mode" ,hbJrcp.getGuaranteeMode());
        }
        if (ObjUtil.isNotNull(hbJrcp.getBeginPublishTime()) && ObjUtil.isNotNull(hbJrcp.getEndPublishTime())){
            queryWrapper.between(TABLE_PREFIX+"publish_time", DateUtil.beginOfDay(hbJrcp.getBeginPublishTime()),DateUtil.endOfDay(hbJrcp.getEndPublishTime()));
        }
        if(hbJrcp.getIsPublished() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_published" ,hbJrcp.getIsPublished());
        }
        if(hbJrcp.getIsDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"is_deleted" ,hbJrcp.getIsDeleted());
        }
        if(StrUtil.isNotBlank(hbJrcp.getApprovalMode())){
            queryWrapper.eq(TABLE_PREFIX+"approval_mode" ,hbJrcp.getApprovalMode());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出金融产品列表
     */
    @PreAuthorize("@ss.hasPermi('manage:jrcp:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, HbJrcpVo hbJrcp,QueryWrapper<HbJrcp> queryWrapper) {
        if (CollUtil.isEmpty( hbJrcp.getIds())){
            setQueryCondition(hbJrcp,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",hbJrcp.getIds());
        }
        List<HbJrcp> list = hbJrcpService.list(queryWrapper);
        ExcelUtil<HbJrcp> util = new ExcelUtil<HbJrcp>(HbJrcp. class);
        util.exportExcel(response,list, "jrcp");
    }

    /**
     * 获取金融产品详细信息
     */
    @ApiOperation(value = "获取金融产品",notes = "金融产品管理")
    @PreAuthorize("@ss.hasPermi('manage:jrcp:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(hbJrcpService.queryGetById(id));
    }

    /**
     * 新增金融产品
     */
    @ApiOperation(value = "新增金融产品",notes = "金融产品管理")
    @PreAuthorize("@ss.hasPermi('manage:jrcp:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HbJrcp hbJrcp) {
        return toAjax(hbJrcpService.addHbJrcp(hbJrcp) ? 1 : 0);
    }

    /**
     * 修改金融产品
     */
    @ApiOperation(value = "修改金融产品",notes = "金融产品管理")
    @PreAuthorize("@ss.hasPermi('manage:jrcp:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody HbJrcp hbJrcp) {
        return toAjax(hbJrcpService.updateHbJrcp(hbJrcp) ? 1 : 0);
    }

    /**
     * 删除金融产品
     */
    @ApiOperation(value = "删除金融产品",notes = "金融产品管理")
    @PreAuthorize("@ss.hasPermi('manage:jrcp:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hbJrcpService.deleteHbJrcpByIds(ids) ? 1 : 0);
    }
}
